package com.hw.one.dam2.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.dao.*;
import com.hw.one.dam2.param.CityAirParam;
import com.hw.one.dam2.param.RegionSiteParam;
import com.hw.one.dam2.param.SiteAirParam;
import com.hw.one.dam2.po.RegionAirQualityPO;
import com.hw.one.dam2.po.SiteAirConcentrationPO;
import com.hw.one.dam2.po.SiteAirUploadRateBasePO;
import com.hw.one.dam2.po.SiteAirUploadRatePO;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.ext.util.UserUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/03/24 10:05.
 * <p>
 * 统计概览业务逻辑实现类
 */
@Service
public class StatisticalOverviewService implements IStatisticalOverviewService {

    @Autowired
    private ICityAirDao cityAirDao;

    @Autowired
    private IWeatherDao weatherDao;

    @Autowired
    private IRegionSiteDao regionSiteDao;

    @Autowired
    private IRegionAirDao regionAirDao;

    @Autowired
    private ISiteAirDao siteAirDao;

    // 数字保留两位小数
    DecimalFormat decimalformat2 = new DecimalFormat("#0.00");
    DecimalFormat decimalformat = new DecimalFormat("#");

    @Override
    public Object findStatisticalOverview(Map map) throws Exception {
        CityAirParam cityAirParam = new CityAirParam();
        cityAirParam.setEndTime(map.get("endTime").toString() + " 00:00:00");
        List<String> regionCodeList = new ArrayList<>();
        regionCodeList.add(map.get("regionCode").toString());
        cityAirParam.setRegionCodes(regionCodeList);
//        Map cityTotalData = cityAirDao.findLastCityMonthTotalByRegionCodeAndTime(cityAirParam);
        RegionAirQualityPO cityTotalData = regionAirDao.findRegionMonthDataByRegionCodeAndTime(cityAirParam);

        //优良天数数据
        JSONObject fineDayData = new JSONObject();
        JSONArray nameList = new JSONArray();
        nameList.add("优");
        nameList.add("良");
        nameList.add("轻度污染");
        nameList.add("中度污染");
        nameList.add("严重污染");
        nameList.add("重度污染");
        //取得空气质量数据
        JSONArray dataList = new JSONArray();

        //污染物分担率数据
        JSONObject shareRateData = new JSONObject();
        JSONArray shareRateList = new JSONArray();

        //首要污染物数据
        JSONObject pollutionData = new JSONObject();
        JSONArray pollutionNameList = new JSONArray();
        JSONArray pollutionDataList = new JSONArray();
        pollutionNameList.add("PM10");
        pollutionNameList.add("PM2.5");
        pollutionNameList.add("SO2");
        pollutionNameList.add("NO2");
        pollutionNameList.add("CO");
        pollutionNameList.add("O3");
        if (null == cityTotalData) {
            for (int i = 0; i < 6; i++) {
                dataList.add("-");
                pollutionDataList.add("-");
                shareRateList.add("-");
            }

        } else {
            Map<String, Integer> AQILevels = cityTotalData.getQUALITYCOLLECT().getAQILEVEL();

            //优良天数
            dataList.add(AQILevels.get("1") == null ? 0 : AQILevels.get("1"));
            dataList.add(AQILevels.get("2") == null ? 0 : AQILevels.get("2"));
            dataList.add(AQILevels.get("3") == null ? 0 : AQILevels.get("3"));
            dataList.add(AQILevels.get("4") == null ? 0 : AQILevels.get("4"));
            dataList.add(AQILevels.get("5") == null ? 0 : AQILevels.get("5"));
            dataList.add(AQILevels.get("6") == null ? 0 : AQILevels.get("6"));

            Map<String, Integer> pollutionMap = cityTotalData.getQUALITYCOLLECT().getPOLLUTANT();

            //污染物次数
            pollutionDataList.add(pollutionMap.get("PM10") == null ? 0 : pollutionMap.get("PM10"));
            pollutionDataList.add(pollutionMap.get("PM2_5") == null ? 0 : pollutionMap.get("PM2_5"));
            pollutionDataList.add(pollutionMap.get("SO2") == null ? 0 : pollutionMap.get("SO2"));
            pollutionDataList.add(pollutionMap.get("NO2") == null ? 0 : pollutionMap.get("NO2"));
            pollutionDataList.add(pollutionMap.get("CO") == null ? 0 : pollutionMap.get("CO"));
            pollutionDataList.add(pollutionMap.get("O3") == null ? 0 : pollutionMap.get("O3"));

            //污染物分担率
            String compoSiteValue = cityTotalData.getCOMPOSITEVALUE();
            String PM10SUM = cityTotalData.getPM10().getSUBINDEX();
            String PM25SUM = cityTotalData.getPM25().getSUBINDEX();
            String SO2SUM = cityTotalData.getSO2().getSUBINDEX();
            String NO2SUM = cityTotalData.getNO2().getSUBINDEX();
            String COSUM = cityTotalData.getCO().getSUBINDEX();
            String O3_8SUM = cityTotalData.getO3().getSUBINDEX();
            if (null == compoSiteValue || Double.valueOf(compoSiteValue) <=0) {
                for (int i=0; i<6; i++) {
                    shareRateList.add("-");
                }
            }else {
                if (null == PM10SUM || Double.valueOf(PM10SUM) <=0) {
                    shareRateList.add("-");
                }else {
                    shareRateList.add(decimalformat.format(Double.valueOf(PM10SUM) / Double.valueOf(compoSiteValue) * 100));
                }

                if (null == PM25SUM || Double.valueOf(PM25SUM) <=0) {
                    shareRateList.add("-");
                }else {
                    shareRateList.add(decimalformat.format(Double.valueOf(PM25SUM) / Double.valueOf(compoSiteValue) * 100));
                }
                if (null == SO2SUM || Double.valueOf(SO2SUM) <=0) {
                    shareRateList.add("-");
                }else {
                    shareRateList.add(decimalformat.format(Double.valueOf(SO2SUM) / Double.valueOf(compoSiteValue) * 100));
                }
                if (null == NO2SUM || Double.valueOf(NO2SUM) <=0) {
                    shareRateList.add("-");
                }else {
                    shareRateList.add(decimalformat.format(Double.valueOf(NO2SUM) / Double.valueOf(compoSiteValue) * 100));
                }
                if (null == COSUM || Double.valueOf(COSUM) <=0) {
                    shareRateList.add("-");
                }else {
                    shareRateList.add(decimalformat.format(Double.valueOf(COSUM) / Double.valueOf(compoSiteValue) * 100));
                }
                if (null == O3_8SUM || Double.valueOf(O3_8SUM) <=0) {
                    shareRateList.add("-");
                }else {
                    shareRateList.add(decimalformat.format(Double.valueOf(O3_8SUM) / Double.valueOf(compoSiteValue) * 100));
                }
            }
        }

        fineDayData.put("nameList", nameList);
        fineDayData.put("dataList", dataList);

        pollutionData.put("pollutionNameList", pollutionNameList);
        pollutionData.put("pollutionDataList", pollutionDataList);

        shareRateData.put("pollutionNameList", pollutionNameList);
        shareRateData.put("shareRateList", shareRateList);
        JSONObject resultData = new JSONObject();
        resultData.put("fineDayData", fineDayData);
        resultData.put("pollutionData", pollutionData);
        resultData.put("shareRateData", shareRateData);
        return resultData;
    }


    @Override
    public Object findPollutionContrastRatio(Map map) {
        CityAirParam cityAirParam = new CityAirParam();
        cityAirParam.setEndTime(map.get("endTime").toString() + " 00:00:00");
        List<String> regionCodeList = new ArrayList<>();
        regionCodeList.add(map.get("regionCode").toString());
        cityAirParam.setRegionCodes(regionCodeList);
//        Map nowTotalData = cityAirDao.findLastCityMonthTotalByRegionCodeAndTime(cityAirParam);
        RegionAirQualityPO nowTotalData = regionAirDao.findRegionMonthDataByRegionCodeAndTime(cityAirParam);


        cityAirParam.setEndTime(map.get("lastYear").toString() + " 00:00:00");
//        Map lastTotalData = cityAirDao.findLastCityMonthTotalByRegionCodeAndTime(cityAirParam);
        RegionAirQualityPO lastTotalData = regionAirDao.findRegionMonthDataByRegionCodeAndTime(cityAirParam);

        //污染物名称
        JSONArray pollutionList = new JSONArray();
        pollutionList.add("AQI");
        pollutionList.add("PM10");
        pollutionList.add("PM2.5");
        pollutionList.add("SO2");
        pollutionList.add("NO2");
        pollutionList.add("CO");
        pollutionList.add("O3");
        //数据 去年和今年
        JSONArray nowDataList = new JSONArray();
        JSONArray lastDataList = new JSONArray();
        //同比值
        JSONArray contrastRatioDataList = new JSONArray();


        String nowAQI = null == nowTotalData || null == nowTotalData.getAQI() ? "-" : nowTotalData.getAQI();
        String nowPM10 = null == nowTotalData || null == nowTotalData.getPM10().getCONCENTRATION() ? "-" : nowTotalData.getPM10().getCONCENTRATION();
        String nowPM25 = null == nowTotalData || null == nowTotalData.getPM25().getCONCENTRATION() ? "-" : nowTotalData.getPM25().getCONCENTRATION();
        String nowSO2 = null == nowTotalData || null == nowTotalData.getSO2().getCONCENTRATION() ? "-" : nowTotalData.getSO2().getCONCENTRATION();
        String nowO3_8 = null == nowTotalData || null == nowTotalData.getO3().getCONCENTRATION() ? "-" : nowTotalData.getO3().getCONCENTRATION();
        String nowNO2 = null == nowTotalData || null == nowTotalData.getNO2().getCONCENTRATION() ? "-" : nowTotalData.getNO2().getCONCENTRATION();
        String nowCO = null == nowTotalData || null == nowTotalData.getCO().getCONCENTRATION() ? "-" : nowTotalData.getCO().getCONCENTRATION();
        nowDataList.add(nowAQI);
        nowDataList.add(nowPM10);
        nowDataList.add(nowPM25);
        nowDataList.add(nowSO2);
        nowDataList.add(nowNO2);
        nowDataList.add(nowCO);
        nowDataList.add(nowO3_8);

        String lastAQI = null == lastTotalData || null == lastTotalData.getAQI() ? "-" : lastTotalData.getAQI();
        String lastPM10 = null == lastTotalData || null == lastTotalData.getPM10().getCONCENTRATION() ? "-" : lastTotalData.getPM10().getCONCENTRATION();
        String lastPM25 = null == lastTotalData || null == lastTotalData.getPM25().getCONCENTRATION() ? "-" : lastTotalData.getPM25().getCONCENTRATION();
        String lastSO2 = null == lastTotalData || null == lastTotalData.getSO2().getCONCENTRATION() ? "-" : lastTotalData.getSO2().getCONCENTRATION();
        String lastO3_8 = null == lastTotalData || null == lastTotalData.getO3().getCONCENTRATION() ? "-" : lastTotalData.getO3().getCONCENTRATION();
        String lastNO2 = null == lastTotalData || null == lastTotalData.getNO2().getCONCENTRATION() ? "-" : lastTotalData.getNO2().getCONCENTRATION();
        String lastCO = null == lastTotalData || null == lastTotalData.getCO().getCONCENTRATION() ? "-" : lastTotalData.getCO().getCONCENTRATION();
        lastDataList.add(lastAQI);
        lastDataList.add(lastPM10);
        lastDataList.add(lastPM25);
        lastDataList.add(lastSO2);
        lastDataList.add(lastNO2);
        lastDataList.add(lastCO);
        lastDataList.add(lastO3_8);


        contrastRatioDataList.add("0".equals(lastAQI) || "-".equals(lastAQI) || "-".equals(nowAQI) ? "-" : decimalformat.format((Double.valueOf(nowAQI) - Double.valueOf(lastAQI)) / Double.valueOf(lastAQI) * 100));
        contrastRatioDataList.add("0".equals(lastPM10) || "-".equals(lastPM10) || "-".equals(nowPM10) ? "-" : decimalformat.format((Double.valueOf(nowPM10) - Double.valueOf(lastPM10)) / Double.valueOf(lastPM10) * 100));
        contrastRatioDataList.add("0".equals(lastPM25) || "-".equals(lastPM25) || "-".equals(nowPM25) ? "-" : decimalformat.format((Double.valueOf(nowPM25) - Double.valueOf(lastPM25)) / Double.valueOf(lastPM25) * 100));
        contrastRatioDataList.add("0".equals(lastSO2) || "-".equals(lastSO2) || "-".equals(nowSO2) ? "-" : decimalformat.format((Double.valueOf(nowSO2) - Double.valueOf(lastSO2)) / Double.valueOf(lastSO2) * 100));
        contrastRatioDataList.add("0".equals(lastNO2) || "-".equals(lastNO2) || "-".equals(nowNO2) ? "-" : decimalformat.format((Double.valueOf(nowNO2) - Double.valueOf(lastNO2)) / Double.valueOf(lastNO2) * 100));
        contrastRatioDataList.add("0".equals(lastCO) || "-".equals(lastCO) || "-".equals(nowCO) ? "-" : decimalformat.format((Double.valueOf(nowCO) - Double.valueOf(lastCO)) / Double.valueOf(lastCO) * 100));
        contrastRatioDataList.add("0".equals(lastO3_8) || "-".equals(lastO3_8) || "-".equals(nowO3_8) ? "-" : decimalformat.format((Double.valueOf(nowO3_8) - Double.valueOf(lastO3_8)) / Double.valueOf(lastO3_8) * 100));
        JSONObject dataObj = new JSONObject();
        dataObj.put("nowDataList", nowDataList);
        dataObj.put("lastDataList", lastDataList);
        dataObj.put("contrastRatioDataList", contrastRatioDataList);

        JSONObject resultData = new JSONObject();
        resultData.put("pollutionList", pollutionList);
        resultData.put("dataObj", dataObj);
        return resultData;
    }

    @Override
    public Object findAirQualityTrend(Map map) {
        CityAirParam cityAirParam = new CityAirParam();
        List<String> regionCodeList = new ArrayList<>();
        regionCodeList.add((String) map.get("regionCode"));
        cityAirParam.setRegionCodes(regionCodeList);
        cityAirParam.setStartTime(map.get("startTime").toString() + " 00:00:00");
        cityAirParam.setEndTime(map.get("endTime").toString() + " 00:00:00");
//        List<Map> resultList = cityAirDao.findCityDayDataByRegionCodeAndTimeASC(cityAirParam);
        List<RegionAirQualityPO> resultList = regionAirDao.findRegionDayDataByRegionCodesAndTimeASC(cityAirParam);
        Map weatherMap = (Map) weatherDao.findCityLiveWeatherByRegionCode((String) map.get("cityRegionCode"));
        //温度
        JSONArray TEMDataList = new JSONArray();
        //湿度
        JSONArray HUMDataList = new JSONArray();
        //风速
//        JSONArray WSDataList = new JSONArray();
        //风向
        JSONArray WDDataList = new JSONArray();
        //风力
        JSONArray WPDataList = new JSONArray();

        JSONArray PM10DataList = new JSONArray();
        JSONArray PM25DataList = new JSONArray();
        JSONArray SO2DataList = new JSONArray();
        JSONArray NO2DataList = new JSONArray();
        JSONArray CODataList = new JSONArray();
        JSONArray O3DataList = new JSONArray();
        JSONArray timeList = new JSONArray();

        //根据开始时间和结束时间以1天为间隔，得到时间点数组
        List<String> monitorTimeList = CalendarUtil.getDatesBetweenTwoDate(map.get("startTime").toString(), map.get("endTime").toString(), "day");
        Boolean flag = false;
        for (String monitorTime : monitorTimeList) {
            flag = false;
            for (RegionAirQualityPO dataMap : resultList) {
                if (monitorTime.equals(dataMap.getMONITORTIME().substring(0, map.get("startTime").toString().length()))) {
                    flag = true;
                    PM10DataList.add(dataMap.getPM10().getCONCENTRATION());
                    PM25DataList.add(dataMap.getPM25().getCONCENTRATION());
                    SO2DataList.add(dataMap.getSO2().getCONCENTRATION());
                    NO2DataList.add(dataMap.getNO2().getCONCENTRATION());
                    CODataList.add(dataMap.getCO().getCONCENTRATION());
                    O3DataList.add(dataMap.getO3().getCONCENTRATION());
                    break;
                }
            }
            // 如果此次循环的时间没有值,加入占位符
            if (!flag) {
                PM10DataList.add("-");
                PM25DataList.add("-");
                SO2DataList.add("-");
                NO2DataList.add("-");
                CODataList.add("-");
                O3DataList.add("-");
            }

            TEMDataList.add(weatherMap == null || weatherMap.get("temperature") == null  ? "-" : weatherMap.get("temperature"));
            HUMDataList.add(weatherMap == null || weatherMap.get("humidity") == null  ? "-" : weatherMap.get("humidity"));
            WDDataList.add(weatherMap == null || weatherMap.get("windDirect") == null  ? "-" : getDegreeByWindDirect((String) weatherMap.get("windDirect")));
            WPDataList.add(weatherMap == null || weatherMap.get("windSpeed") == null  ? "-" : weatherMap.get("windSpeed"));
        }

        JSONArray name = new JSONArray();
        name.add("温度");
        name.add("相对湿度");
        name.add("风力");
        name.add("风向");
        name.add("PM10");
        name.add("PM2.5");
        name.add("SO2");
        name.add("NO2");
        name.add("CO");
        name.add("O3");


        JSONArray dataList = new JSONArray();
        dataList.add(TEMDataList);
        dataList.add(HUMDataList);
        dataList.add(WPDataList);
        dataList.add(WDDataList);
        dataList.add(PM10DataList);
        dataList.add(PM25DataList);
        dataList.add(SO2DataList);
        dataList.add(NO2DataList);
        dataList.add(CODataList);
        dataList.add(O3DataList);

        JSONObject resultObj = new JSONObject();
        resultObj.put("timeList", monitorTimeList);
        resultObj.put("dataList", dataList);
        resultObj.put("nameList", name);
        return resultObj;
    }

    @Override
    public Object findAreaAirQualityAnalysis(Map map) {

        List<String> regionCodeList = new ArrayList<>();
        Map regionParamMap = new HashMap();
        regionParamMap.put("sortScorpeType", "areaCountySort");
        regionParamMap.put("regionCode", map.get("regionCode"));
        List<Map<String, Object>> regionList = regionSiteDao.findRegionListByRegionType(regionParamMap);
        JSONArray areaList = new JSONArray();
        for (Map regionMap : regionList) {
            if ("22".equals(regionMap.get("regionType"))) {
                regionCodeList.add((String) regionMap.get("regionCode"));
                areaList.add(regionMap.get("regionName"));
            }
        }

        JSONArray nameList = new JSONArray();
        nameList.add("浓度值");
        nameList.add("同比");
        nameList.add("环比");

        CityAirParam cityAirParam = new CityAirParam();
        cityAirParam.setEndTime(map.get("endTime") + " 00:00:00");
        cityAirParam.setRegionCodes(regionCodeList);
//        List<Map> cityDayList = cityAirDao.findLastCityMonthTotalByRegionCodeAndTimeList(cityAirParam);
        List<RegionAirQualityPO> cityDayList = regionAirDao.findRegionMonthDataByRegionCodesAndTime(cityAirParam);

//        JSONArray factor = new JSONArray();
//        factor.add("AQI");
//        factor.add("综合指数");
//        factor.add("PM10");
//        factor.add("PM2.5");
//        factor.add("SO2");
//        factor.add("NO2");
//        factor.add("CO");
//        factor.add("O3");

        JSONObject nowData = new JSONObject();
        JSONArray nowAQIList = new JSONArray();
        JSONArray nowCompoSiteValueList = new JSONArray();
        JSONArray nowPM10List = new JSONArray();
        JSONArray nowPM25List = new JSONArray();
        JSONArray nowSO2List = new JSONArray();
        JSONArray nowNO2List = new JSONArray();
        JSONArray nowCOList = new JSONArray();
        JSONArray nowO3List = new JSONArray();
        for (String regionCode : regionCodeList) {
            for (RegionAirQualityPO cityMap : cityDayList) {
                if (regionCode.equals(cityMap.getREGIONCODE())) {
                    nowAQIList.add(cityMap.getAQI());
                    nowCompoSiteValueList.add(cityMap.getCOMPOSITEVALUE());
                    nowPM10List.add(cityMap.getPM10().getCONCENTRATION());
                    nowPM25List.add(cityMap.getPM25().getCONCENTRATION());
                    nowSO2List.add(cityMap.getSO2().getCONCENTRATION());
                    nowNO2List.add(cityMap.getNO2().getCONCENTRATION());
                    nowCOList.add(cityMap.getCO().getCONCENTRATION());
                    nowO3List.add(cityMap.getO3().getCONCENTRATION());
                }
            }
        }
        nowData.put("AQI", nowAQIList);
        nowData.put("COMPOSITEVALUE", nowCompoSiteValueList);
        nowData.put("PM10", nowPM10List);
        nowData.put("PM25", nowPM25List);
        nowData.put("SO2", nowSO2List);
        nowData.put("NO2", nowNO2List);
        nowData.put("CO", nowCOList);
        nowData.put("O3", nowO3List);


        JSONArray averageList = new JSONArray();
        averageList.add(76);
        averageList.add(23);
        averageList.add(45);
        //浓度值数据
        JSONArray concentrationList = new JSONArray();
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            concentrationList.add(random.nextInt(100));
        }

        //同比数据
        JSONArray yoyList = new JSONArray();
        for (int i = 0; i < 8; i++) {
            yoyList.add(random.nextInt(100));
        }

        //环比数据
        JSONArray chainRatioList = new JSONArray();
        for (int i = 0; i < 8; i++) {
            chainRatioList.add(random.nextInt(100));
        }

        JSONArray dataList = new JSONArray();
        dataList.add(concentrationList);
        dataList.add(yoyList);
        dataList.add(chainRatioList);

        JSONObject dataObj = new JSONObject();
        dataObj.put("nameList", nameList);
        dataObj.put("areaList", areaList);
        dataObj.put("dataList", dataList);
        dataObj.put("averageList", averageList);
        return dataObj;
    }

    @Override
    public Object findAreaAirQualityAnalysisByFactor(Map map) {
        List<String> regionCodeList = new ArrayList<>();

        Map<String, String> regionParamMap = new HashMap<>();
        regionParamMap.put("sortScorpeType", "areaCountySort");
        regionParamMap.put("regionCode", map.get("regionCode").toString());
        List<Map<String, Object>> regionList = regionSiteDao.findRegionListByRegionType(regionParamMap);
        JSONArray areaList = new JSONArray();
        for (Map regionMap : regionList) {
            if ("22".equals(regionMap.get("regionType"))) {
                regionCodeList.add((String) regionMap.get("regionCode"));
                areaList.add(regionMap.get("regionName"));
            }
        }


        String factor = (String) map.get("factor");
        CityAirParam cityAirParam = new CityAirParam();
        cityAirParam.setStartTime(map.get("endTime") + " 00:00:00");
        cityAirParam.setEndTime(map.get("endTime") + " 00:00:00");
        cityAirParam.setRegionCodes(regionCodeList);
//        List<Map> cityDayList = cityAirDao.findLastCityMonthTotalByRegionCodeAndTimeList(cityAirParam);
        List<RegionAirQualityPO> cityDayList = regionAirDao.findRegionMonthDataByRegionCodesAndTime(cityAirParam);


        JSONArray dataList = new JSONArray();

        JSONArray IAQIList = new JSONArray();
        for (String regionCode : regionCodeList) {
            boolean flag = false;
            for (RegionAirQualityPO cityMap : cityDayList) {
                if (regionCode.equals(cityMap.getREGIONCODE())) {
                    switch (factor) {
                        case "AQI":
                            dataList.add(cityMap.getAQI());
                            IAQIList.add(cityMap.getAQI());
                            break;
                        case "COMPOSITEVALUE":
                            dataList.add(cityMap.getCOMPOSITEVALUE());
                            IAQIList.add(cityMap.getCOMPOSITEVALUE());
                            break;
                        case "PM10":
                            dataList.add(cityMap.getPM10().getCONCENTRATION());
                            IAQIList.add(cityMap.getPM10().getSUBINDEX());
                            break;
                        case "PM25":
                            dataList.add(cityMap.getPM25().getCONCENTRATION());
                            IAQIList.add(cityMap.getPM25().getSUBINDEX());
                            break;
                        case "SO2":
                            dataList.add(cityMap.getSO2().getCONCENTRATION());
                            IAQIList.add(cityMap.getSO2().getSUBINDEX());
                            break;
                        case "NO2":
                            dataList.add(cityMap.getNO2().getCONCENTRATION());
                            IAQIList.add(cityMap.getNO2().getSUBINDEX());
                            break;
                        case "CO":
                            dataList.add(cityMap.getCO().getCONCENTRATION());
                            IAQIList.add(cityMap.getCO().getSUBINDEX());
                            break;
                        case "O3":
                            dataList.add(cityMap.getO3().getCONCENTRATION());
                            IAQIList.add(cityMap.getO3().getSUBINDEX());
                            break;
                    }
                    flag = true;
                }
            }
            if (!flag) {
                dataList.add("-");
                IAQIList.add("-");
            }
        }
        JSONObject airData = new JSONObject();
        airData.put("airList", dataList);
        airData.put("IAQIList", IAQIList);

        //前一年的数据
        cityAirParam.setEndTime(map.get("lastYear") + " 00:00:00");
        cityAirParam.setStartTime(map.get("lastYear") + " 00:00:00");
//        List<Map> lastCityDayList = cityAirDao.findLastCityMonthTotalByRegionCodeAndTimeList(cityAirParam);
        List<RegionAirQualityPO> lastCityDayList = regionAirDao.findRegionYearDataByRegionCodesAndTime(cityAirParam);
        JSONArray lastDataList = new JSONArray();
        for (String regionCode : regionCodeList) {
            boolean flag = false;
            for (RegionAirQualityPO cityMap : lastCityDayList) {
                if (regionCode.equals(cityMap.getREGIONCODE())) {
                    switch (factor) {
                        case "AQI":
                            lastDataList.add(cityMap.getAQI());
                            flag = true;
                            break;
                        case "COMPOSITEVALUE":
                            lastDataList.add(cityMap.getCOMPOSITEVALUE());
                            flag = true;
                            break;
                        case "PM10":
                            lastDataList.add(cityMap.getPM10().getCONCENTRATION());
                            flag = true;
                            break;
                        case "PM25":
                            lastDataList.add(cityMap.getPM25().getCONCENTRATION());
                            flag = true;
                            break;
                        case "SO2":
                            lastDataList.add(cityMap.getSO2().getCONCENTRATION());
                            flag = true;
                            break;
                        case "NO2":
                            lastDataList.add(cityMap.getNO2().getCONCENTRATION());
                            flag = true;
                            break;
                        case "CO":
                            lastDataList.add(cityMap.getCO().getCONCENTRATION());
                            flag = true;
                            break;
                        case "O3":
                            lastDataList.add(cityMap.getO3().getCONCENTRATION());
                            flag = true;
                            break;
                    }
//                    lastDataList.add(cityMap.get(factor));
                }
            }
            if (!flag) {
                lastDataList.add("-");
            }
        }
        JSONObject yoyData = new JSONObject();
        //同比数据
        JSONArray yoyList = new JSONArray();
        for (int i = 0; i < dataList.size(); i++) {
            if ("-".equals(dataList.get(i).toString()) || lastCityDayList.size() == 0 || null == lastCityDayList.get(i)
                || "-".equals(lastDataList.get(i).toString())
                || "0".equals(lastDataList.get(i).toString())) {
                yoyList.add("-");
            } else {
                Double airD = Double.valueOf(dataList.get(i).toString());
                Double lastYearD = Double.valueOf(lastDataList.get(i).toString());
                yoyList.add(decimalformat2.format((airD - lastYearD) / lastYearD * 100));
            }

        }
        yoyData.put("yoyList", yoyList);


        //前一天的数据
        cityAirParam.setEndTime(map.get("yesterday") + " 00:00:00");
//        List<Map> yesterdayCityDayList = cityAirDao.findLastCityMonthTotalByRegionCodeAndTimeList(cityAirParam);
        List<RegionAirQualityPO> yesterdayCityDayList = regionAirDao.findRegionMonthDataByRegionCodesAndTime(cityAirParam);

        JSONArray yesterdayDataList = new JSONArray();
        for (String regionCode : regionCodeList) {
            boolean flag = false;
            for (RegionAirQualityPO cityMap : yesterdayCityDayList) {
                if (regionCode.equals(cityMap.getREGIONCODE())) {
                    switch (factor) {
                        case "AQI":
                            yesterdayDataList.add(cityMap.getAQI());
                            flag = true;
                            break;
                        case "COMPOSITEVALUE":
                            yesterdayDataList.add(cityMap.getCOMPOSITEVALUE());
                            flag = true;
                            break;
                        case "PM10":
                            yesterdayDataList.add(cityMap.getPM10().getCONCENTRATION());
                            flag = true;
                            break;
                        case "PM25":
                            yesterdayDataList.add(cityMap.getPM25().getCONCENTRATION());
                            flag = true;
                            break;
                        case "SO2":
                            yesterdayDataList.add(cityMap.getSO2().getCONCENTRATION());
                            flag = true;
                            break;
                        case "NO2":
                            yesterdayDataList.add(cityMap.getNO2().getCONCENTRATION());
                            flag = true;
                            break;
                        case "CO":
                            yesterdayDataList.add(cityMap.getCO().getCONCENTRATION());
                            flag = true;
                            break;
                        case "O3":
                            yesterdayDataList.add(cityMap.getO3().getCONCENTRATION());
                            flag = true;
                            break;
                    }
                }
            }
            if (!flag) {
                yesterdayDataList.add("-");
            }
        }
        JSONObject momData = new JSONObject();
        //环比数据
        JSONArray momList = new JSONArray();
        for (int i = 0; i < dataList.size(); i++) {
            if ("-".equals(dataList.get(i).toString()) || yesterdayDataList.size() == 0 || null == yesterdayDataList.get(i)
                || "-".equals(yesterdayDataList.get(i).toString())
                || "0".equals(yesterdayDataList.get(i).toString())) {
                momList.add("-");
            } else {
                Double airD = Double.valueOf(dataList.get(i).toString());
                Double yesterdayD = Double.valueOf(yesterdayDataList.get(i).toString());
                momList.add(decimalformat2.format((airD - yesterdayD) / yesterdayD * 100));
            }
        }
        momData.put("momList", momList);

        JSONObject resultData = new JSONObject();
        resultData.put("areaList", areaList);
        resultData.put("airData", airData);
        resultData.put("yoyData", yoyData);
        resultData.put("momData", momData);
        return resultData;
    }

    @Override
    public Object findSiteAirUploadRate(HttpServletRequest request) throws Exception{
        String pointType = request.getParameter("pointType");
        String regionCode = request.getParameter("regionCode");
        String siteCodes = request.getParameter("siteCode");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String searchType = request.getParameter("searchType");

        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return new ArrayList<>();
        }

        if (StringUtils.isEmpty(searchType)) {
            searchType = "uploadRate";
        }
        Map<String, Object> region = UserUtil.getRegion();
        String userRegionCode = (String) region.get("regionCode");
        String userRegionName = (String) region.get("regionName");

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, -1);
        SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.SDF_YMD_HMS);
        // 如果查询的结束数据大于上个小时时间,就修改时间为上个小时时间
        if (calendar.getTime().getTime() < sdf.parse(endTime).getTime()) {
            endTime = sdf.format(calendar.getTime());
        }
        // 组装查询参数
        RegionSiteParam siteParam = new RegionSiteParam();
        if (StringUtils.isEmpty(regionCode)) {
            siteParam.setRegionCode(userRegionCode);
        }else {
            siteParam.setRegionCode(regionCode);
        }
        if (StringUtils.isEmpty(pointType)) {
            siteParam.setSiteType(SiteTypeEnum.ALL_AIR_SITE.getValue());
        }else {
            siteParam.setSiteType(pointType);
        }
        if (!StringUtils.isEmpty(siteCodes)) {
            siteParam.setSiteCodeList(Arrays.asList(siteCodes.split(",")));
        }
        // 查询站点信息
        List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);

        // 站点编码列表
        List<String> siteCodeList = new ArrayList<>();
        for (Map<String, Object> siteInfo : siteInfoList) {
            siteCodeList.add((String) siteInfo.get("siteCode"));
        }
        SiteAirParam siteAirParam = new SiteAirParam();
        siteAirParam.setStartTime(startTime);
        siteAirParam.setEndTime(endTime);
        siteAirParam.setSiteCodes(siteCodeList);
        // 查询数据
        List<SiteAirConcentrationPO> hourDataList = siteAirDao.findSiteAirHourConcentrationDataBySiteCodesAndTime(siteAirParam);

        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);
        // 应传数
        double hours = (double) ((endDate.getTime() - startDate.getTime())/(1000*60*60)) + 1;
        List<SiteAirUploadRatePO> resultList = new ArrayList<>();
        // 循环数据,计算上传数
        for (Map<String, Object> siteInfo : siteInfoList) {
            String siteCode = (String) siteInfo.get("siteCode");
            double pm10UploadIndex = 0;
            double pm10ValidIndex = 0;
            double pm25UploadIndex = 0;
            double pm25ValidIndex = 0;
            double so2UploadIndex = 0;
            double so2ValidIndex = 0;
            double no2UploadIndex = 0;
            double no2ValidIndex = 0;
            double coUploadIndex = 0;
            double coValidIndex = 0;
            double o3UploadIndex = 0;
            double o3ValidIndex = 0;
            // 加上 这个参数,用来过滤数据库中可能会出现重复数据的现象
            String lastMonitorTime = "";
            for (SiteAirConcentrationPO hourData : hourDataList) {
                if (siteCode.equals(hourData.getSITECODE())) {
                    String monitorTime = hourData.getMONITORTIME();
                    if (lastMonitorTime.equals(monitorTime)) {
                        continue;
                    }
                    if ("uploadRate".equals(searchType)) {
                        if (!StringUtils.isEmpty(hourData.getPM10())) {
                            pm10UploadIndex ++;
                        }
                        if (!StringUtils.isEmpty(hourData.getPM25())) {
                            pm25UploadIndex ++;
                        }
                        if (!StringUtils.isEmpty(hourData.getSO2())) {
                            so2UploadIndex ++;
                        }
                        if (!StringUtils.isEmpty(hourData.getNO2())) {
                            no2UploadIndex ++;
                        }
                        if (!StringUtils.isEmpty(hourData.getCO())) {
                            coUploadIndex ++;
                        }
                        if (!StringUtils.isEmpty(hourData.getO3())) {
                            o3UploadIndex ++;
                        }
                    }
                    if ("validRate".equals(searchType)) {
                        if (!StringUtils.isEmpty(hourData.getPM10())) {
                            pm10UploadIndex ++;
                            if (Integer.valueOf(hourData.getPM10()) > 0) {
                                pm10ValidIndex ++;
                            }
                        }
                        if (!StringUtils.isEmpty(hourData.getPM25())) {
                            pm25UploadIndex ++;
                            if (Integer.valueOf(hourData.getPM25()) > 0) {
                                pm25ValidIndex ++;
                            }
                        }
                        if (!StringUtils.isEmpty(hourData.getSO2())) {
                            so2UploadIndex ++;
                            if (Integer.valueOf(hourData.getSO2()) > 0) {
                                so2ValidIndex ++;
                            }
                        }
                        if (!StringUtils.isEmpty(hourData.getNO2())) {
                            no2UploadIndex ++;
                            if (Integer.valueOf(hourData.getNO2()) > 0) {
                                no2ValidIndex ++;
                            }
                        }
                        if (!StringUtils.isEmpty(hourData.getCO())) {
                            coUploadIndex ++;
                            if (Double.valueOf(hourData.getCO()) > 0) {
                                coValidIndex ++;
                            }
                        }
                        if (!StringUtils.isEmpty(hourData.getO3())) {
                            o3UploadIndex ++;
                            if (Integer.valueOf(hourData.getO3()) > 0) {
                                o3ValidIndex ++;
                            }
                        }
                    }

                    lastMonitorTime = monitorTime;
                }
            }
            DecimalFormat df_1 = new DecimalFormat("###.#");


            if ("uploadRate".equals(searchType)) {
                // 组装数据
                String shouldNum = String.valueOf((int)hours);
                SiteAirUploadRatePO ratePO = new SiteAirUploadRatePO();
                ratePO.setSiteCode(siteCode);
                ratePO.setSiteName((String) siteInfo.get("siteName"));
                ratePO.setRegionCode((String) siteInfo.get("regionCode"));
                ratePO.setRegionName((String) siteInfo.get("RegionName"));

                SiteAirUploadRateBasePO pm10Rate = new SiteAirUploadRateBasePO();
                pm10Rate.setShouldNum(shouldNum);
                pm10Rate.setValidNum(String.valueOf((int)pm10UploadIndex));
                pm10Rate.setValidRate(df_1.format(pm10UploadIndex/hours* 100));

                SiteAirUploadRateBasePO pm25Rate = new SiteAirUploadRateBasePO();
                pm25Rate.setShouldNum(shouldNum);
                pm25Rate.setValidNum(String.valueOf((int)pm25UploadIndex));
                pm25Rate.setValidRate(df_1.format(pm25UploadIndex/hours* 100));

                SiteAirUploadRateBasePO so2Rate = new SiteAirUploadRateBasePO();
                so2Rate.setShouldNum(shouldNum);
                so2Rate.setValidNum(String.valueOf((int)so2UploadIndex));
                so2Rate.setValidRate(df_1.format(so2UploadIndex/hours* 100));

                SiteAirUploadRateBasePO no2Rate = new SiteAirUploadRateBasePO();
                no2Rate.setShouldNum(shouldNum);
                no2Rate.setValidNum(String.valueOf((int)no2UploadIndex));
                no2Rate.setValidRate(df_1.format(no2UploadIndex/hours* 100));

                SiteAirUploadRateBasePO coRate = new SiteAirUploadRateBasePO();
                coRate.setShouldNum(shouldNum);
                coRate.setValidNum(String.valueOf((int)coUploadIndex));
                coRate.setValidRate(df_1.format(coUploadIndex/hours* 100));

                SiteAirUploadRateBasePO o3Rate = new SiteAirUploadRateBasePO();
                o3Rate.setShouldNum(shouldNum);
                o3Rate.setValidNum(String.valueOf((int)o3UploadIndex));
                o3Rate.setValidRate(df_1.format(o3UploadIndex/hours * 100));
                ratePO.setPM10(pm10Rate);
                ratePO.setPM2_5(pm25Rate);
                ratePO.setSO2(so2Rate);
                ratePO.setNO2(no2Rate);
                ratePO.setCO(coRate);
                ratePO.setO3(o3Rate);

                resultList.add(ratePO);
            }
            if ("validRate".equals(searchType)) {
                // 组装数据
                SiteAirUploadRatePO ratePO = new SiteAirUploadRatePO();
                ratePO.setSiteCode(siteCode);
                ratePO.setSiteName((String) siteInfo.get("siteName"));
                ratePO.setRegionCode((String) siteInfo.get("regionCode"));
                ratePO.setRegionName((String) siteInfo.get("RegionName"));

                SiteAirUploadRateBasePO pm10Rate = new SiteAirUploadRateBasePO();
                pm10Rate.setShouldNum(String.valueOf((int)pm10UploadIndex));
                pm10Rate.setValidNum(String.valueOf((int)pm10ValidIndex));
                pm10Rate.setValidRate(df_1.format(pm10ValidIndex/pm10UploadIndex* 100));

                SiteAirUploadRateBasePO pm25Rate = new SiteAirUploadRateBasePO();
                pm25Rate.setShouldNum(String.valueOf((int)pm25UploadIndex));
                pm25Rate.setValidNum(String.valueOf((int)pm25ValidIndex));
                pm25Rate.setValidRate(df_1.format(pm25ValidIndex/pm25UploadIndex* 100));

                SiteAirUploadRateBasePO so2Rate = new SiteAirUploadRateBasePO();
                so2Rate.setShouldNum(String.valueOf((int)so2UploadIndex));
                so2Rate.setValidNum(String.valueOf((int)so2ValidIndex));
                so2Rate.setValidRate(df_1.format(so2ValidIndex/so2UploadIndex* 100));

                SiteAirUploadRateBasePO no2Rate = new SiteAirUploadRateBasePO();
                no2Rate.setShouldNum(String.valueOf((int)no2UploadIndex));
                no2Rate.setValidNum(String.valueOf((int)no2ValidIndex));
                no2Rate.setValidRate(df_1.format(no2ValidIndex/no2UploadIndex* 100));

                SiteAirUploadRateBasePO coRate = new SiteAirUploadRateBasePO();
                coRate.setShouldNum(String.valueOf((int)coUploadIndex));
                coRate.setValidNum(String.valueOf((int)coValidIndex));
                coRate.setValidRate(df_1.format(coValidIndex/coUploadIndex* 100));

                SiteAirUploadRateBasePO o3Rate = new SiteAirUploadRateBasePO();
                o3Rate.setShouldNum(String.valueOf((int)o3UploadIndex));
                o3Rate.setValidNum(String.valueOf((int)o3ValidIndex));
                o3Rate.setValidRate(df_1.format(o3ValidIndex/o3UploadIndex * 100));
                ratePO.setPM10(pm10Rate);
                ratePO.setPM2_5(pm25Rate);
                ratePO.setSO2(so2Rate);
                ratePO.setNO2(no2Rate);
                ratePO.setCO(coRate);
                ratePO.setO3(o3Rate);
                resultList.add(ratePO);
            }



        }
        JSONObject obj = new JSONObject();
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        obj.put("dataList", resultList);
        obj.put("searchType", searchType);
        return obj;
    }


    private int getDegreeByWindDirect(String windDirect) {
        int degree = 0;
        if ("东风".equals(windDirect)) {
            degree = 90;
        } else if ("东南风".equals(windDirect)) {
            degree = 135;
        } else if ("东北风".equals(windDirect)) {
            degree = 45;
        } else if ("西风".equals(windDirect)) {
            degree = 270;
        } else if ("西南风".equals(windDirect)) {
            degree = 225;
        } else if ("西北风".equals(windDirect)) {
            degree = 315;
        } else if ("南风".equals(windDirect)) {
            degree = 180;
        } else if ("北风".equals(windDirect)) {
            degree = 360;
        }
        return degree;
    }


}
